<?php

namespace App\Admin\Controllers\Chart;

use App\Models\Order;
use App\Models\RecordOrder;
use App\Models\Room;
use Carbon\Carbon;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Column;
use Encore\Admin\Layout\Content;
use Encore\Admin\Layout\Row;
use Encore\Admin\Widgets\Box;
use Illuminate\Http\Request;
use Rap2hpoutre\FastExcel\Facades\FastExcel;
use Rap2hpoutre\FastExcel\SheetCollection;

class TurnoverChartController
{
	public function index(Content $content)
	{
		$data = [
			'oneDay' => $this->Chart('day'),
			'oneMonth' => $this->Chart('month'),
			'oneYear' => $this->Chart('year'),
		];
		
		return $content
			->header('营业统计')
			->title('营业统计')
			//->row(view('admin.export.export-btn', ['route' => route('turnoverExport')]))
			->row(function (Row $row) use ($data) {
				
				$row->column(11, function (Column $column) use ($data) {
					$column->row(new Box('今日', view('admin.chart.turnoverChart', ['name' => 'oneDay', 'x' => $data['oneDay']['x'], 'data' => $data['oneDay']['data']])));
					$column->row(new Box('本月', view('admin.chart.turnoverChart', ['name' => 'oneMonth', 'x' => $data['oneMonth']['x'], 'data' => $data['oneMonth']['data']])));
					$column->row(new Box('今年', view('admin.chart.turnoverChart', ['name' => 'oneYear', 'x' => $data['oneYear']['x'], 'data' => $data['oneYear']['data']])));
				});
			});
	}
	
	public function Chart($index)
	{
		$timeStart = new Carbon();
		$timeEnd = new Carbon();
		$timeBetween = [];
		switch ($index) {
			case 'day':
				$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
				$raw = 'hour';
				$x = countHour();
				break;
			case 'month':
				$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
				$raw = 'Day';
				$x = countX($timeStart->daysInMonth);
				break;
			case 'year':
				$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
				$raw = 'Month';
				$x = countX(12);
				break;
			
			default:
				$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
				break;
		}
		
		$recordOrder = RecordOrder::whereBetween('created_at', $timeBetween)
			->selectRaw($raw . '(created_at) as date,sum(cash_price) as cash_price, sum(alipay_price) as alipay_price, sum(wxpay_price) as wxpay_price, sum(private_price) as private_price')
			->groupBy('date')
			->get();
	
		foreach ($x as $k => $v) {
			$y['现金']['value'][$k] = 0;
			$y['现金']['rgb'] = 'rgba(255,156,0,0.2)';
			$y['支付宝']['value'][$k] = 0;
			$y['支付宝']['rgb'] = 'rgba(3,162,156,0.2)';
			$y['微信']['value'][$k] = 0;
			$y['微信']['rgb'] = 'rgba(128,184,46,0.2)';
			$y['余额']['value'][$k] = 0;
			$y['余额']['rgb'] = 'rgba(224,55,96,0.2)';
			foreach ($recordOrder as $key => $value) {
				if ($value['date'] == $v) {
					$y['现金']['value'][$k] = $value['cash_price'] / 100;
					$y['支付宝']['value'][$k] = $value['alipay_price'] / 100;
					$y['微信']['value'][$k] = $value['wxpay_price'] / 100;
					$y['余额']['value'][$k] = $value['private_price'] / 100;
				}
			}
		}
		//dd($y);
		return $res = [
			'x' => $x,
			'data' => $y,
		];
	}
	
	public function export(Request $request)
	{
		$time = new Carbon();
		switch ($request->get('date')) {
			case 'year':
				return FastExcel::data($this->exportData('year'))
					->download('营业.' . $time->format('Y') . '年.xlsx');
				break;
			case 'month':
				return FastExcel::data($this->exportData('month'))
					->download('营业.' . $time->format('Y-m') . '月.xlsx');
				break;
			case 'day':
				return FastExcel::data($this->exportData('day'))
					->download('营业.' . $time->format('Y-m-d') . '.xlsx');
				break;
		}
	}
	
	public function exportData($index = 'year')
	{
		$timeStart = new Carbon();
		$timeEnd = new Carbon();
		$timeBetween = [];
		
		switch ($index) {
			case 'day':
				$timeBetween = [$timeStart->startOfDay(), $timeEnd->endOfDay()];
				$raw = 'Hour';
				$data = $this->collection($raw, $timeBetween);
				break;
			case 'month':
				$timeBetween = [$timeStart->startOfMonth(), $timeEnd->endOfMonth()];
				$raw = 'day';
				$data = $this->collection($raw, $timeBetween);
				break;
			case 'year':
				$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
				$raw = 'month';
				$data = $this->collection($raw, $timeBetween);
				break;
			
			default:
				$timeBetween = [$timeStart->startOfYear(), $timeEnd->endOfYear()];
				break;
		}
		
		return $data;
	}
	
	public function collection($raw, $timeBetween)
	{
		switch ($raw){
			case 'year':
				$as = '月份';
				break;
			case 'month':
				$as = '日期';
				break;
			case 'day':
				$as = '时间';
				break;
		}
		 $y = new SheetCollection([
			'现金' => recordOrderChart(RecordOrder::whereBetween('created_at', $timeBetween)
					->selectRaw($raw.'(created_at) as '.$as.',sum(cash_price) as 金额')
					->groupBy($as)
					->get()),
			'微信' => recordOrderChart(RecordOrder::whereBetween('created_at', $timeBetween)
					->selectRaw($raw.'(created_at) as '.$as.',sum(wxpay_price) as 金额')
					->groupBy($as)
					->get()),
			'支付宝' => recordOrderChart(RecordOrder::whereBetween('created_at', $timeBetween)
					->selectRaw($raw.'(created_at) as '.$as.',sum(alipay_price) as 金额')
					->groupBy($as)
					->get()),
			'余额' => recordOrderChart(RecordOrder::whereBetween('created_at', $timeBetween)
					->selectRaw($raw.'(created_at) as '.$as.',sum(private_price) as 金额')
					->groupBy($as)
					->get()),
		]);
		//dd($y);
		return $y;
	}
}
